package com.ifd.gui.as400;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import com.ibm.as400.access.SpooledFile;
import com.ifd.gui.JTextFieldLimit;

public class VSpooledFileSelection extends JDialog implements ActionListener,
		KeyListener {

	public static SpooledFile getSelectedSpooledFile() {
		VSpooledFileSelection selection = new VSpooledFileSelection();
		selection.setModal(true);
		selection.setVisible(true);
		return selection.selectedSplf;
	}

	static final Logger logger = Logger.getLogger(VSpooledFileSelection.class);
	private SpooledFile selectedSplf;

	private JTextField jtfUser;
	private JTextField jtfJobNum;
	private JTextField jtfJobUser;
	private JTextField jtfJobName;
	private JTextField jtfSplfName;
	private JTextField jtfUserData;
	private JTextField jtfFormType;
	private JLabel libraryLabel;
	private JTextField jtfOuqLib;
	private JTextField jtfOutq;

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	/*
	 * public static void main(String args[]) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { try { UIManager
	 * .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
	 * com.jgoodies.looks.Options .setPopupDropShadowEnabled(true);
	 * PropertyConfigurator.configure("lib/log4j.conf"); } catch (Exception e) {
	 * System.out.println("Cannot load Look and Feel"); }
	 * 
	 * VSpooledFileSelection dialog = new VSpooledFileSelection();
	 * dialog.addWindowListener(new WindowAdapter() { public void
	 * windowClosing(WindowEvent e) { IFDConnectionPool.closePool();
	 * System.exit(0); } }); dialog.setVisible(true); } catch (Exception e) {
	 * e.printStackTrace(); } } }); }
	 */
	/**
	 * Create the dialog
	 */
	public VSpooledFileSelection() {
		super();
		setSize(new Dimension(250, 420));
		buildLayout();
		setLocationRelativeTo(null);
		setTitle("Work With Spooled Files");
	}

	private void buildLayout() {
		getContentPane().setLayout(null);

		final JLabel selectSpoolOutq = new JLabel();
		selectSpoolOutq.setText("Select Spooled Files List by Output Queue");
		selectSpoolOutq.setBounds(10, 10, 215, 20);
		getContentPane().add(selectSpoolOutq);

		final JLabel outputQueueLabel = new JLabel();
		outputQueueLabel.setText("Output queue");
		outputQueueLabel.setBounds(20, 35, 80, 20);
		getContentPane().add(outputQueueLabel);

		jtfOutq = new JTextField();
		jtfOutq.setBounds(105, 35, 100, 20);
		jtfOutq.setDocument(new JTextFieldLimit(10, true));
		jtfOutq.addKeyListener(this);
		getContentPane().add(jtfOutq);

		libraryLabel = new JLabel();
		libraryLabel.setText("Library");
		libraryLabel.setBounds(30, 60, 70, 20);
		getContentPane().add(libraryLabel);

		jtfOuqLib = new JTextField();
		jtfOuqLib.setDocument(new JTextFieldLimit(10, true));
		jtfOuqLib.setBounds(105, 60, 100, 20);
		jtfOuqLib.setText("*LIBL");
		jtfOuqLib.addKeyListener(this);

		getContentPane().add(jtfOuqLib);

		final JLabel orSelectByLabel = new JLabel();
		orSelectByLabel.setText("Or Select by Spool Attributes");
		orSelectByLabel.setBounds(10, 100, 215, 20);
		getContentPane().add(orSelectByLabel);

		final JLabel userLabel = new JLabel();
		userLabel.setText("User");
		userLabel.setBounds(20, 125, 80, 20);
		getContentPane().add(userLabel);

		jtfUser = new JTextField();
		jtfUser.setBounds(105, 125, 100, 20);
		jtfUser.setDocument(new JTextFieldLimit(10, true));
		jtfUser.setText("*CURRENT");
		jtfUser.addKeyListener(this);
		getContentPane().add(jtfUser);

		final JLabel formTypeLabel = new JLabel();
		formTypeLabel.setText("Form type");
		formTypeLabel.setBounds(20, 150, 80, 20);
		getContentPane().add(formTypeLabel);

		jtfFormType = new JTextField();
		jtfFormType.setBounds(105, 150, 100, 20);
		jtfFormType.setDocument(new JTextFieldLimit(10));
		jtfFormType.setText("*ALL");
		jtfFormType.addKeyListener(this);
		getContentPane().add(jtfFormType);

		final JLabel userDataLabel = new JLabel();
		userDataLabel.setText("User Data");
		userDataLabel.setBounds(20, 175, 80, 20);
		getContentPane().add(userDataLabel);

		jtfUserData = new JTextField();
		jtfUserData.setBounds(105, 175, 100, 20);
		jtfUserData.setDocument(new JTextFieldLimit(10));
		jtfUserData.setText("*ALL");
		jtfUserData.addKeyListener(this);
		getContentPane().add(jtfUserData);

		final JLabel spoolFileNameLabel = new JLabel();
		spoolFileNameLabel.setText("Spool File Name");
		spoolFileNameLabel.setBounds(20, 200, 80, 20);
		getContentPane().add(spoolFileNameLabel);

		jtfSplfName = new JTextField();
		jtfSplfName.setBounds(105, 200, 100, 20);
		jtfSplfName.setDocument(new JTextFieldLimit(10, true));
		jtfSplfName.setText("*ALL");
		jtfSplfName.addKeyListener(this);
		getContentPane().add(jtfSplfName);

		final JLabel orSelectByJob = new JLabel();
		orSelectByJob.setText("Or Select by Job");
		orSelectByJob.setBounds(10, 240, 100, 20);
		getContentPane().add(orSelectByJob);

		final JLabel jobNameLabel = new JLabel();
		jobNameLabel.setText("Job Name");
		jobNameLabel.setBounds(20, 266, 80, 20);
		getContentPane().add(jobNameLabel);

		jtfJobName = new JTextField();
		jtfJobName.setBounds(105, 266, 100, 20);
		jtfJobName.setDocument(new JTextFieldLimit(10, true));
		jtfJobName.setText("*ALL");
		jtfJobName.addKeyListener(this);
		getContentPane().add(jtfJobName);

		final JLabel jobUserLabel = new JLabel();
		jobUserLabel.setText("User");
		jobUserLabel.setBounds(30, 292, 70, 20);
		getContentPane().add(jobUserLabel);

		jtfJobUser = new JTextField();
		jtfJobUser.setBounds(105, 292, 100, 20);
		jtfJobUser.setDocument(new JTextFieldLimit(10, true));
		jtfJobUser.addKeyListener(this);
		getContentPane().add(jtfJobUser);

		final JLabel numberLabel = new JLabel();
		numberLabel.setText("Number");
		numberLabel.setBounds(30, 318, 70, 20);
		getContentPane().add(numberLabel);

		jtfJobNum = new JTextField();
		jtfJobNum.setBounds(105, 318, 100, 20);
		jtfJobNum.setDocument(new JTextFieldLimit(6, false, true));
		jtfJobNum.addKeyListener(this);
		getContentPane().add(jtfJobNum);

		final JButton jbtLoad = new JButton("Load");
		jbtLoad.setBounds(10, 350, 90, 25);
		jbtLoad.addActionListener(this);
		jbtLoad.setMnemonic(KeyEvent.VK_L);
		jbtLoad.setActionCommand("load");
		jbtLoad.addKeyListener(this);
		getContentPane().add(jbtLoad);

		final JButton jbtCancel = new JButton("Cancel");
		jbtCancel.setMnemonic(KeyEvent.VK_C);
		jbtCancel.setBounds(116, 350, 90, 25);
		jbtCancel.setActionCommand("cancel");
		jbtCancel.addActionListener(this);
		jbtCancel.addKeyListener(this);
		getContentPane().add(jbtCancel);
	}

	public void actionPerformed(ActionEvent e) {
		try {
			// TODO Auto-generated method stub
			String command = e.getActionCommand();
			if (command.equalsIgnoreCase("cancel")) {
				this.dispose();
			} else if (command.equalsIgnoreCase("load")) {
				loadButton();
			}
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, ex.getLocalizedMessage(),
					"Spooled File Selection", JOptionPane.OK_OPTION);
		}
	}

	private void loadButton() {
		VSpooledFileList spList = new VSpooledFileList();
		buildFilter(spList);
		try {
			selectedSplf = spList.getSelectedSpooledFile();
		} catch (Exception e1) {
			selectedSplf = null;
		}
		this.dispose();
	}

	private void buildFilter(VSpooledFileList list) {
		// Set attributes to retrieve
		/*
		 * int[] attributes = { SpooledFile.ATTR_JOBNAME,
		 * SpooledFile.ATTR_JOBNUMBER, SpooledFile.ATTR_JOBUSER,
		 * SpooledFile.ATTR_SPOOLFILE, //The name of the spooled file.
		 * SpooledFile.ATTR_SPLFNUM, //The spooled file number.
		 * SpooledFile.ATTR_FORMTYPE, SpooledFile.ATTR_USERDATA, //User data
		 * SpooledFile.ATTR_PRTDEVTYPE, //The printer data stream type. Valid
		 * values areSCS,IPDS,USERASCII,AFPDS,LINE. SpooledFile.ATTR_SPLF_SIZE,
		 * //The spooled file size SpooledFile.ATTR_SPLFSTATUS, //The status of
		 * the spooled file SpooledFile.ATTR_DESCRIPTION, //
		 * SpooledFile.ATTR_OUTPUT_QUEUE, SpooledFile.ATTR_STARTPAGE,
		 * SpooledFile.ATTR_ENDPAGE, SpooledFile.ATTR_PAGES,
		 * SpooledFile.ATTR_PAGEWIDTH, SpooledFile.ATTR_PAGELEN,
		 * SpooledFile.ATTR_CONTROLCHAR, SpooledFile.ATTR_COPIES,
		 * SpooledFile.ATTR_CODEPAGE};
		 */

		String outq = jtfOutq.getText().trim();
		String oqlb = jtfOuqLib.getText();

		String spUser = jtfUser.getText();
		String spForm = jtfFormType.getText();
		String spData = jtfUserData.getText();
		String spFile = jtfSplfName.getText();

		String jobName = jtfJobName.getText();
		String jobUser = jtfJobUser.getText();
		String jobNmbr = jtfJobNum.getText();

		if (jtfOutq.getText().trim().length() > 0) { // Get spool list by output
														// queue
			list.setQueueFilter(outq, oqlb);
			list.setUserFilter("*ALL");
		} else if (jobName.length() > 0 && jobUser.length() > 0
				&& jobNmbr.length() > 0) { // Get spool list by job
			list.setUserFilter("*ALL");
		} else { // Get spool list by user
			if (spUser.trim().length() > 0) {
				list.setUserFilter(spUser.toUpperCase());
			}

			if (spFile.trim().length() > 0)
				list.setSpoolNameFilter(spFile);

			if (spForm.trim().length() > 0)
				list.setFormTypeFilter(spForm);

			if (spData.trim().length() > 0)
				list.setUserDataFilter(spData);
		}
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_ENTER) {
			loadButton();
		}
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}
}
